<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/jquery-1.12.4.js"></script>
</head>

<body>

    <div class="catbox">
        <table id="cartTable">
            <thead>
                <tr>
                    <th>
                        <label for="">
                            <input class="check-all" type="checkbox">&nbsp;&nbsp;全选
                        </label>
                    <th>商品</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                    </th>
                </tr>
            </thead>
            <tbody>

            </tbody>

        </table>
        <div class="foot" id="foot">

            <a class="fl" id="deleteAll" href="javascript:;">删除</a>
            <div class="fr closing" onclick="getTotal();">结 算</div>
            <input type="hidden" id="cartTotalPrice" />
            <div class="fr total">合计：￥<span id="priceTotal">0.00</span></div>
            <div class="fr selected" id="selected">已选商品<span id="selectedTotal">0</span>件<span
                    class="arrow up">︽</span><span class="arrow down">︾</span></div>
            <div class="selected-view">
                <div id="selectedViewList" class="clearfix">
                    <div><img src=""><span>取消选择</span></div>
                </div>
                <span class="arrow">◆<span>◆</span></span>
            </div>
        </div>
    </div>

    <script>
        var tbody = document.querySelector("#cartTable tbody");
        $.ajax({
            type: "get",
            url: "../data/data.json",
            dataType: "json",
            success(data) {
                console.log(data);
                var html = ``
                data.forEach(({ goodsName, goodsPrice, goodsImg, buyNum }) => html += `
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="${goodsImg}" alt="" /><span>${goodsName}</span></td>
                    <td class="price">${goodsPrice.toFixed(2)}</td>
                    <td class="count">
                        <span class="reduce">${buyNum > 1 ? "-" : ""}</span>
                        <input class="count-input" type="text" value="${buyNum}" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">${(buyNum * goodsPrice).toFixed(2)}</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                `)
                $("#cartTable tbody").html(html)

            }
        })
        // 全选功能
        // 所有的单选 跟随全选的状态
        $(document).on("click", ".check-all", function () {
            var status = $(this).prop("checked");
            $(".check-one").prop("checked", status)
            getTotal()
        })

        // 反选功能
        $(document).on("click", ".check-one", function () {
            var flag = $(".check-one").get().every(function (ele) {
                return ele.checked == true
            })
            $(".check-all").prop("checked", flag)
            getTotal()

        })

        //滑动禁用效果
        $(document).on("mouseenter", ".reduce", function () {
            var num = $(this).next().val();
            if (num == 1) {
                $(this).addClass("disabled")
            } else {
                $(this).removeClass("disabled")
            }
        })
        //加法
        $(document).on("click", ".add", function () {
            let num = $(this).prev().val() * 1;
            // console.log(num);
            $(this).prev().val(num + 1)
            var price = $(this).parent().prev().text();
            var total = (num * price).toFixed(2);
            $(this).parent().next().text(total)
            $(this).prevAll(".reduce").text("-")
            getTotal()
        })
        //减法
        $(document).on("click", ".reduce", function () {
            let num = $(this).next().val() * 1;
            // console.log(num);
            //强制停止
            if (num == 1) return false;
            num--;
            if (num == 1) {
                $(this).html("")
                $(this).addClass(".disabled")
            };
            $(this).next().val(num)
            var price = $(this).parent().prev().text();
            var total = (num * price).toFixed(2);
            $(this).parent().next().text(total)
            getTotal()

        })
        //单删
        $(document).on("click", ".delete", function () {
            if (confirm("是否删除该商品？")) {
                $(this).parents("tr").remove();
                isAllChecked()
                getTotal()
            }
        })
        //多删
        $(document).on("click", "#deleteAll", function () {
            if (confirm("是否删除选中商品？")) {
                $(".check-one:checked").parents("tr").remove()
                isAllChecked()
                getTotal()
            }
        })

        function isAllChecked() {
            var flag = $(".check-one").is(":not(:checked)");
            $(".check-all").prop("checked", $(".check-one").length == 0 ? false : !flag)
        }
        function getTotal() {
            //结算
            var sum = 0;
            var allPrice = 0;
            $(".check-one:checked").each(function () {
                var num = $(this).parents("tr").find(".count-input").val() * 1
                var total = $(this).parents("tr").find(".subtotal").text() * 1
                sum += num;
                allPrice += total;
            })
            $("#selectedTotal").html(sum);
            $("#priceTotal").html(allPrice.toFixed(2))
        }






    </script>
</body>

</html>